﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CodeGeneratorBootstrap.ClassServices;
using CodeGeneratorBootstrap.Generators;

namespace CodeGeneratorBootstrap.Models
{
	public class ClassModel
	{
		public int Id { get; set; }
		public string Name { get; set; }
		public List<AttributeModel> _attributes;
        public List<ButtonModel> Buttons;
		public int ProjectId { get; set; }
        public ClassTypeModel MultiDimensionStyle { get; set; }
        public List<ClassModel> ParentClass { get; set; }
        public List<ClassModel> ChildrenClass { get; set; }

        // Start add for summary field
        public AttributeModel GetMasterAttributeForSummaryByChildClass(ClassModel childClassModel)
        {
            var SummaryChildClassList = _attributes.FindAll(x => x.IsMasterDetail);
            if (SummaryChildClassList != null && SummaryChildClassList.Count() != 0)
            {
                foreach (var attributeModel in SummaryChildClassList)
                {
                    var DestClassId = Generator.GetDestinationClassByAttribute(attributeModel).Id;

                    if (DestClassId == childClassModel.Id)
                    {
                        return attributeModel;

                    }
                    else { return new AttributeModel(); }

                }
                return null;
            }
            else
            {
                return null;
            }

        }


        public List<AttributeModel> GetCalculationAttribute() // Return all attribute that's summary-rollup field
        {
            var calculationAttribute = _attributes.FindAll(x => x.DataType.Name == "Summary-Sum" ||
                                                x.DataType.Name == "Summary-Count" ||
                                                x.DataType.Name == "Summary-Min" ||
                                                x.DataType.Name == "Summary-Max");
            if (calculationAttribute.Count() != 0 && calculationAttribute != null)
            {

                return calculationAttribute;
            }
            else
            {
                return null;
            }
        }
        // End add for summary field
		internal ClassModel(ClassModelDto dto)
		{
			Id = dto.Id;
			Name = dto.Name;	
			ProjectId = dto.ProjectId;
            MultiDimensionStyle = new ClassTypeModel(dto.MultiDimensionStyle);
			_attributes = new List<AttributeModel>();
            Buttons = new List<ButtonModel>();

            if (dto.Attributes != null)
                foreach (AttributeModelDto attridto in dto.Attributes)
                {
                    if (attridto.DataType.Name.Equals("Button") || attridto.FieldType.Name.Equals("Button"))
                    {
                        ButtonModel buttonModel = new ButtonModel(attridto);
                        this.Buttons.Add(buttonModel);
                    }
                    else
                    {
                        AttributeModel attributeModel = new AttributeModel(attridto);
                        this._attributes.Add(attributeModel);
                    }
                }
		}

        internal void SetAttributes(List<AttributeModelDto> list)
        {
            _attributes = new List<AttributeModel>();
            Buttons = new List<ButtonModel>();

            if (list != null)
                foreach (AttributeModelDto attridto in list)
                {
                    if (attridto.DataType.Name.Equals("Button") || attridto.FieldType.Name.Equals("Button"))
                    {
                        ButtonModel buttonModel = new ButtonModel(attridto);
                        this.Buttons.Add(buttonModel);
                    }
                    else
                    {
                        AttributeModel attributeModel = new AttributeModel(attridto);
                        this._attributes.Add(attributeModel);
                    }
                }
        }

		public ClassModel()
		{
			_attributes = new List<AttributeModel>();
		}

		public void AddAttribute(AttributeModel attribute)
		{
			_attributes.Add(attribute);
		}

		public List<AttributeModel> GetAttributes()
		{
			return _attributes;
		}

		public ClassModel GetAllPrimaryKey()
		{
			ClassModel newDto = new ClassModel();
			newDto.Id = this.Id;
			newDto.Name = this.Name;

			newDto._attributes = _attributes.FindAll(a => a.Pk == true);

			return newDto;
		}

		public ClassModel GetAllSearchable()
		{
			ClassModel newDto = new ClassModel();
			newDto.Id = this.Id;
			newDto.Name = this.Name;

			newDto._attributes = _attributes.FindAll(a => a.Search == true);

			return newDto;
		}

		public ClassModel GetAllDisplay()
		{
			ClassModel newDto = new ClassModel();
			newDto.Id = this.Id;
			newDto.Name = this.Name;

			newDto._attributes = _attributes.FindAll(a => a.Display == true);

			return newDto;
		}

		public ClassModel GetAllDisplayInGrid()
		{
			ClassModel newDto = new ClassModel();
			newDto.Id = this.Id;
			newDto.Name = this.Name;

			newDto._attributes = _attributes.FindAll(a => a.DisplayLengthInGrid > 0);

			return newDto;
		}

		public ClassModel GetAllNotNullable()
		{
			ClassModel newDto = new ClassModel();
			newDto.Id = this.Id;
			newDto.Name = this.Name;

			newDto._attributes = _attributes.FindAll(a => a.Nullable == false);

			return newDto;
		}

		public ClassModel GetAllNullable()
		{
			ClassModel newDto = new ClassModel();
			newDto.Id = this.Id;
			newDto.Name = this.Name;

			newDto._attributes = _attributes.FindAll(a => a.Nullable == true);

			return newDto;
		}

		/*public ClassModel GetAllDataType(int dataTypeId)
		{
			ClassModel newDto = new ClassModel();
			newDto.Id = this.Id;
			newDto.Name = this.Name;

			newDto._attributes = _attributes.FindAll(a => a.DataType.TypeId == dataTypeId);

			return newDto;
		}

		public ClassModel GetAllFieldType(int fieldTypeId)
		{
			ClassModel newDto = new ClassModel();
			newDto.Id = this.Id;
			newDto.Name = this.Name;

			newDto._attributes = _attributes.FindAll(a => a.FieldType.TypeId == fieldTypeId);

			return newDto;
		}*/

		public ClassModel GetAllSearchResult()
		{
			ClassModel newDto = new ClassModel();
			newDto.Id = this.Id;
			newDto.Name = this.Name;

            newDto._attributes = _attributes.FindAll(a => a.LinkEdit == true);

			return newDto;
		}

		public ClassModel GetAllDateTime() //Apple Add Method
		{
			ClassModel newDto = new ClassModel();
			newDto.Id = this.Id;
			newDto.Name = this.Name;

			newDto._attributes = _attributes.FindAll(a => a.DataType.Name == "DateTime");

			return newDto;
		}
		public ClassModel GetAllLookup() //Apple Add Method
		{
			ClassModel newDto = new ClassModel();
			newDto.Id = this.Id;
			newDto.Name = this.Name;

			newDto._attributes = _attributes.FindAll(a => a.IsLookup);

			return newDto;
		}

        public bool IsLookup(List<LookupClassModel> list)
        {
            foreach (AttributeModel a in _attributes)
            {
                if (a.GetLookup(list).Id > 0)
                    return true;
            }
            return false;
        }

        public ClassModel GetAllMasterDetail() //Wee
        {
            ClassModel newDto = new ClassModel();
            newDto.Id = this.Id;
            newDto.Name = this.Name;

            newDto._attributes = _attributes.FindAll(a => a.IsMasterDetail);

            return newDto;
        }

        //Weerachai 12-01-2014
        private List<LookupModel> lookupModels = null;
        //Soravit 11-2-2014
        /*public List<LookupModel> GetLookupModelByDestClass()
        {
            if (this.lookupModels == null)
                this.lookupModels = Generator.GetLookupModelByDestClass(this.Id);
            return this.lookupModels;
        }*/
        public List<LookupModel> GetLookupModelByDestClass()
        {
            if (this.lookupModels == null)
            {
                if (ChildrenClass.Count > 0)
                {
                    foreach (AttributeModel attr in ChildrenClass[0]._attributes)
                    {
                        if (attr.IsLookup && attr.LookupClass.Id == this.Id)
                        {
                            this.lookupModels = attr.GetLookupModel();
                            break;
                        }
                    }
                }
            }
            return this.lookupModels;
        }



	}
}
